package entity

import (
	"gorm.io/gorm"
	"inspector/internal/enums"
	v1 "inspector/pkg/meta/v1"
	"inspector/pkg/util/idutil"
)

type Event struct {
	v1.ObjectMeta
	EventType       string            `gorm:"column:event_type;"`
	EventId         string            `gorm:"column:event_id;"`
	EventName       string            `gorm:"column:event_name;"`
	EventStatus     enums.EventStatus `gorm:"column:event_status;"`
	CommitId        string            `gorm:"column:commit_id;"`
	Repository      string            `gorm:"column:repository;"`
	RepositoryId    string            `gorm:"column:repository_id;"`
	Ref             string            `gorm:"column:ref;"`
	Organization    string            `gorm:"column:organization;"`
	OrganizationId  string            `gorm:"column:organization_id;"`
	InstallationId  int64             `gorm:"column:installation_id;"`
	Sender          string            `gorm:"column:sender;"`
	SenderType      string            `gorm:"column:sender_type;"`
	RequestPayload  string            `gorm:"column:request_payload;"`
	ResponsePayload string            `gorm:"column:response_payload;"`
	RetryCount      int               `gorm:"column:retry_count;"`
}

func (n *Event) TableName() string {
	return "app_event"
}

// AfterCreate run after create database record.
func (n *Event) AfterCreate(tx *gorm.DB) error {
	if n.InstanceID == "" {
		n.InstanceID = idutil.GetInstanceID("", 12)
	}
	return tx.Save(n).Error
}

type EventListOptions struct {
	EventType   string            `json:"eventType"`
	EventStatus enums.EventStatus `json:"eventStatus"`
	RetryCount  int               `json:"retryCount"`
}
